import datetime
import multiprocessing
from util import *
import copy
from CloudSystem import run_once
import gc

def generate_default_args():
    args=Args()
    args.request_num=10000
    args.out_dir="output_state_resource"
    args.time_expand=0.1
    args.priority_queue=True
    args.spec_reliability=0.9999
    return args


def run_once_this(method,  state_resource_percent,  trace_id, bare_metal_node_num):
    print(f"start once {method}, {state_resource_percent}, {trace_id}, {bare_metal_node_num}")
    args=generate_default_args()
    
    args.deploy_schedule_strategy=method
    args.state_resource_percent=state_resource_percent
    args.bare_metal_node_num=bare_metal_node_num
    args.trace_id=trace_id
    args_copy=copy.deepcopy(args)
    run_once(args_copy)
    gc.collect()
    print(f"End once {method}, {state_resource_percent}, {trace_id}, {bare_metal_node_num}")
    
    



task_args_list=[]
for bare_metal_node_num in [40,30,20,10]:
    for method in ["RR", "C_GM", "GSMS", "QFEC", "R_RIR", "ours"]:
        for state_resource_percent in [i/10 for i in range(10)]:
            if method in ["RR","C_GM","GSMS","QFEC","R_RIR"] and state_resource_percent!=0:
                continue
            for trace_id in [0,1,2,3]:
                task_args_list.append((method, state_resource_percent, trace_id, bare_metal_node_num))
        
                
with multiprocessing.Pool(processes=32) as pool:
        #   starmap 直接开始所有的
        #   apply_async 一个一个参数放入执行
        
        interval=0
        results=[]
        index=0
        while index<len(task_args_list):
            # length=min(batch_size, len(task_args_list)-index )
            result=pool.apply_async(run_once_this, task_args_list[index])
            results.append(result)
            if index!=0 and index%5==0:
                time.sleep(interval)
            index+=1
            
        for result in results:
            result.get()



print("End all sub threadings")
